package com.jr.service.impl;

import com.jr.mapper.UserMapper;
import com.jr.pojo.LUser;
import com.jr.pojo.PersonResume;
import com.jr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;

@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public LUser login(LUser u) {
        return userMapper.selectLUser(u).get(0);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean register(LUser u) {
        //注册的同时将注册的信息添加到resume表
        PersonResume pr = new PersonResume();
        pr.setUsername(u.getUsername());
        pr.setEmail(u.getEmail());
        return userMapper.insertLUser(u)>0&&userMapper.insertPersonInfo(pr)>0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updatePsw(LUser u) {
        return userMapper.updatePsw(u)>0;
    }

    //查询个人信息
    @Override
    public PersonResume queryPersonInfo(LUser user) {
        return userMapper.selectPersonInfo(user);
    }

    //更新个人信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updatePersonInfo(PersonResume pr) {
        if(pr.getEmail()!=null){
            LUser u = new LUser();
            u.setUsername(pr.getUsername());
            u.setEmail(pr.getEmail());
            return  userMapper.updatePersonInfo(pr)>0&&userMapper.updateEmail(u)>0;
        }
        return userMapper.updatePersonInfo(pr)>0;
    }

    @Override
    public int queryVip(Integer uid) {
        return userMapper.selectVip(uid);
    }




}
